package com.yhty.service.impl;

import javax.annotation.Resource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yhty.dao.PayDao;
import com.yhty.model.PayMent;
import com.yhty.model.Rent;
import com.yhty.service.OweManager;
import com.yhty.service.PayManager;

/**
 * 缴费业务逻辑处理实现
 * 
 * @author RoySong 2011-3-29-下午04:17:12
 */
@Service("payManager")
@Transactional
public class PayManagerImpl implements PayManager {

	private PayDao payDao;
	private OweManager oweManager;

	/*
	 * 缴纳费用
	 * 
	 * @see com.yhty.service.PayManager#add(com.yhty.model.PayMent)
	 */
	public Void pay(PayMent payMent) {
		Rent rent = new Rent();
		rent.setId(payMent.getRent_id());
		PayMent payMentInDb;
		//更新本月欠费信息
		oweManager.updateOwe(payMent);
		try {
			payMentInDb = payDao.getByRent(rent);
		} catch (EmptyResultDataAccessException e) {
			//本月无缴费记录,则直接插入
			payDao.add(payMent);
			return null;
		}
		//本月有缴费记录,则更新记录
		payMent.setId(payMentInDb.getId());
		if (payMent.getPay() != null) {//更新房租物管水电
			payDao.updatePay(payMent);
		} else if (payMent.getPrepare() != null) {//更新预交
			payDao.updatePrepare(payMent);
		} else if (payMent.getOwe() != null) {//更新欠费缴纳
			payDao.updateOwe(payMent);
		}
		return null;
	}

	@Resource(name = "payDao")
	public void setPayDao(PayDao payDao) {
		this.payDao = payDao;
	}

	/*
	 * 根据出租信息获取本月缴费记录
	 * 
	 * @see com.yhty.service.PayManager#getByRent(com.yhty.model.Rent)
	 */
	public PayMent getByRent(Rent rent) {
		PayMent p = null;
		try{
			p = payDao.getByRent(rent);
		}catch(EmptyResultDataAccessException e){
			return null;
		}
		return p;
	}

	@Resource(name="oweManager")
	public void setOweManager(OweManager oweManager) {
		this.oweManager = oweManager;
	}

}
